package My.project.Ui.Activity;


import My.project.Ui.R;
import My.project.Ui.Fragment.SearchVideoFragment;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;

/**
 * This activity displays the results of searching a keyword.
 * It is consist of a list fragment.
 * @author Bell
 */
public class ResultActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.result);
		
		getActionBar().setDisplayHomeAsUpEnabled(true);
		
		// Check that the activity is using the layout version with
        // the fragment FrameLayout
        if (findViewById(R.id.fragment) != null) {

            // However, if we're being restored from a previous state,
            // then we don't need to do anything and should return or else
            // we could end up with overlapping fragments.
            if (savedInstanceState != null) {
                return;
            }

            // Create an instance of ExampleFragment
            SearchVideoFragment firstFragment = new SearchVideoFragment();
            
            // In case this activity was started with special instructions from an Intent,
            // pass the Intent's extras to the fragment as arguments
            firstFragment.setArguments(getIntent().getExtras());
            
            // Add the fragment to the 'fragment' FrameLayout
            getFragmentManager().beginTransaction()
                    .add(R.id.fragment, firstFragment).commit();
        }
	}
	
	//menu button action
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		menu.add(0, 0, 0, R.string.str_about);
		menu.add(0, 1, 1, R.string.str_exit);
	    return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		super.onOptionsItemSelected(item);
		switch(item.getItemId()){
		case android.R.id.home:
            // This is called when the Home (Up) button is pressed
            // in the Action Bar.
            Intent parentActivityIntent = new Intent(
            		this, SearchActivity.class);
            parentActivityIntent.addFlags(
                    Intent.FLAG_ACTIVITY_CLEAR_TOP |
                    Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(parentActivityIntent);
            finish();
            break;
		case 0:
			openOptionsDialog();
			break;
		case 1:
			exitOptionsDialog();
			break;
		default:
			break;
		}
		return true;
	}
	
	// show author info
	private void openOptionsDialog(){
		new AlertDialog.Builder(this).setTitle(R.string.str_about)
		.setMessage(R.string.app_about_msg)
		.setPositiveButton(R.string.str_ok, new DialogInterface.OnClickListener(){
			public void onClick(DialogInterface dialoginterface, int i){
				
			}
		}).show();
	}

	// show exit dialog
	private void exitOptionsDialog(){
		new AlertDialog.Builder(this).setTitle(R.string.str_exit)
		.setMessage(R.string.app_exit_msg)
		.setNegativeButton(R.string.str_no, new DialogInterface.OnClickListener(){
			public void onClick(DialogInterface dialoginterface, int i){
				
			}
		})
		.setPositiveButton(R.string.str_yes, new DialogInterface.OnClickListener(){
			public void onClick(DialogInterface dialoginterface, int i){
				Intent intent = new Intent(Intent.ACTION_MAIN);
  	            intent.addCategory(Intent.CATEGORY_HOME);
  	            intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
  	            startActivity(intent);
  	            android.os.Process.killProcess(android.os.Process.myPid());
			}
		}).show();
	}
}
